(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.createInitialController', createInitialController);
	createInitialController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'resourceBundleService' ];
	function createInitialController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, resourceBundleService) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.resourceBundle = resourceBundleInfo;
		
		ctrl.init = function() {
			authService.createInitial().then(function(response) {
				ctrl.refBundleOptions = response.data.bundleList;
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response.data;
			});
		};

		ctrl.createInitialSave = function() {
			return authService.createInitialSave(ctrl.resourceBundle).then(saveSuccessful, saveFailed);
		};

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.resourceBundle, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					if(ctrl.resourceBundle.copyFromReferenceBundle){
						routerInfo.copyFromReferenceBundle = ctrl.resourceBundle.copyFromReferenceBundle[0];
					}
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.scopeChanged = function() {
			if(ctrl.resourceBundle.scope){
				resourceBundleService.getlocaleCodeOptions(ctrl.resourceBundle.scope).then(function(response) {
					if(response){
						ctrl.localOptions = response.data;
					}
				});
			}
		};
	}
})(angular);